package com.zzyl.service.impl;

import cn.hutool.http.HttpUtil;
import com.zzyl.constant.Constants;
import com.zzyl.dto.UserLoginRequestDto;
import com.zzyl.entity.Member;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.MemberMapper;
import com.zzyl.properties.JwtTokenManagerProperties;
import com.zzyl.properties.WechatConfigProperties;
import com.zzyl.service.MemberService;
import com.zzyl.service.WechatService;
import com.zzyl.utils.JwtUtil;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.LoginVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.scanner.Constant;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class MemberServiceImpl implements MemberService {


    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private WechatService wechatService;
    @Autowired
    private JwtTokenManagerProperties jwtTokenManagerProp;



    @Override
    public Long insertMember(Member member) {
        return memberMapper.insert(member);
    }

    @Override
    public int updateMember(Member member) {
        return memberMapper.update(member);
    }

    @Override
    public int deleteMember(Long memberId) {
        return memberMapper.deleteById(memberId);
    }

    @Override
    public Member getMemberById(Long memberId) {
        return memberMapper.selectById(memberId);
    }

    @Override
    public List<Member> getAllMembers() {
        return memberMapper.selectAll();
    }



    @Override
    public LoginVo login(UserLoginRequestDto dto) {
        //调用微信接口 获取openId
        String openId = wechatService.getOpenId(dto.getCode());
        log.info("获取的openId:{}",openId);
        if (openId == null){
            log.error("openId为空");
            throw new BaseException(BasicEnum.GET_OPENID_ERROR);
        }
        //如果openId不存在 创建Member对象 设置openId
        Member member = memberMapper.selectByOpenId(openId);
        if (ObjectUtil.isEmpty(member)){
            member = new Member();
            member.setOpenId(openId);
        }
        //调用接口 获取手机号
        String phoneNum = wechatService.getPhone(dto.getPhoneCode());
        //判断手机号是否存在 不存在 设置手机号
        if (member.getId() == null){
            member.setPhone(phoneNum);
            //截取手机号的后四位 作为微信昵称
            String nickName = phoneNum.substring(phoneNum.length() - 4);
            member.setName(nickName);
            memberMapper.insert(member);
        }else {
            //如果手机号不一致就修改手机号
            if (!member.getPhone().equals(phoneNum)){
                member.setPhone(phoneNum);
                memberMapper.update(member);
            }
        }

        //生成token 把用户id和昵称放入token 返回小程序
        Map<String, Object> claims = new HashMap<>();
        claims.put(Constants.JWT_USERID,member.getId());
        claims.put(Constants.JWT_USERNAME,member.getName());
        String token = JwtUtil.createJWT(jwtTokenManagerProp.getBase64EncodedSecretKey(),jwtTokenManagerProp.getTtl(),claims);
        //将token设置到LoginVo中
        LoginVo loginVo = new LoginVo();
        loginVo.setToken(token);
        loginVo.setNickName(member.getName());
        return loginVo;
    }
}
